package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageResult<User> list(UserDto userDto) {
        PageHelper.startPage(userDto.getPage(), userDto.getPageSize());
        List<User> userList = userMapper.listUsers(userDto);
        Page<User> page = (Page<User>) userList;
        return new PageResult<>(page.getTotal(), page.getResult());
    }

    @Override
    public void addUser(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        String password = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.addUser(user);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    @Override
    public void updateUser(User user) {
        String password = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }

    @Override
    public List<User> listUserByRole(String roleLabel) {
        return userMapper.listUserByRole(roleLabel);
    }

    @Override
    public List<User> listUserByDept(Integer deptId) {
        return userMapper.ListUserByDept(deptId);
    }

    @Override
    public LoginResultVo login(User user) {
        User u = userMapper.getByUsername(user.getUsername());
        if (u == null || !u.getPassword().equals(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()))){
            log.info("用户名或密码错误");
            throw new BusinessException("用户名或密码错误");
        }
        if (u.getStatus() == 0){
            log.info("用户被禁用");
            throw new BusinessException("用户被禁用");
        }
        LoginResultVo vo = new LoginResultVo();
        vo.setId(u.getId());
        vo.setUsername(u.getUsername());
        vo.setName(u.getName());
        vo.setImage(u.getImage());
        vo.setRoleLabel(u.getRoleLabel());

        Map<String, Object> claims = new HashMap<>();
        claims.put("id", u.getId());
        claims.put("username", u.getUsername());
        String jwt = JwtUtils.generateToken(claims);
        vo.setToken(jwt);
        return vo;
    }
}
